<template>
  <div class="JADP-common-layout">
    <div class="JADP-common-layout-center">
      <el-row class="JADP-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="班级名称">
              <el-input v-model="query.clazzname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="用户名">
              <el-input
                v-model="query.account"
                placeholder="请输入用户名"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input
                v-model="query.realname"
                placeholder="请输入姓名"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>

          <template v-if="showAll">
            <el-col :span="6">
              <el-form-item label="手机号码">
                <el-input
                  v-model="query.mobilephone"
                  placeholder="请输入正确的手机号码"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单位类型">
                <el-select
                  v-model="query.companytype"
                  placeholder="请选择单位类型"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in companytypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="单位名称">
                <el-input
                  v-model="query.company"
                  placeholder="请输入单位名称"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JADP-common-layout-main JADP-flex-main">
        <div class="JADP-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button type="text" icon="el-icon-download" @click="exportData()"
              >导出
            </el-button>
            <!-- <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleBatchRemoveDel()"
              >批量删除
            </el-button> -->
          </div>
          <div class="JADP-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JADP-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull :isContainer="true" />
            <!-- <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options JADP-common-head-icon"
                :underline="false"
              />
            </el-tooltip> -->
          </div>
        </div>
        <JADP-table
          v-loading="listLoading"
          :data="list"
          @sort-change="sortChange"
          has-c
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            prop="clazzname"
            label="班级名称"
            align="center"
            show-overflow-tooltip
          >
            <!-- <template #default="{ row }">
              <ShowAllText :row="row.clazzname" :num="10" />
            </template> -->
          </el-table-column>
          <el-table-column prop="account" label="用户名" align="center" />
          <el-table-column prop="realname" label="姓名" align="center" />
          <el-table-column prop="gender" label="性别" align="center">
            <template v-slot="{ row }">
              {{ getDict("sex", row.gender) }}
            </template>
          </el-table-column>
          <el-table-column prop="mobilephone" label="手机号码" align="center" />
          <el-table-column
            prop="certificatesnumber"
            label="身份证号码"
            width="150"
            align="center"
          />
          <el-table-column
            prop="title"
            label="职称"
            align="center"
          ></el-table-column>
          <el-table-column prop="education" label="学历" align="center">
            <template v-slot="{ row }">
              {{ getDict("Education", row.education) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="company"
            label="工作单位"
            align="center"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column prop="companytype" label="单位类型" align="center">
            <template v-slot="{ row }">
              {{ getDict("dwType", row.companytype) }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="accountstatus" label="账号状态" align="center">
            <template v-slot="{ row }">
              {{ getDict("accountStatu", row.accountstatus) }}
            </template>
          </el-table-column>
          <el-table-column prop="qualification" label="资格审查" align="center">
            <template v-slot="{ row }">
              {{ getDict("zgsc", row.qualification) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="expirationtime"
            label="过期时间"
            align="center"
          /> -->
          <!-- <el-table-column
            prop="creatortime"
            label="创建时间"
            align="center"
            sortable="custom"
          /> -->
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <!-- <el-button
                type="text"
                class="JADP-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button> -->
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id, true)"
                >详情
              </el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <tableOpts
                @edit="addOrUpdateHandle(scope.row.id)"
                @del="handleDel(scope.row.id)"
              >
                <el-dropdown hide-on-click>
                  <span class="el-dropdown-link">
                    <el-button size="mini" type="text">
                      {{ $t("common.moreBtn")
                      }}<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="
                        handleResetPwd(scope.row.id, scope.row.account)
                      "
                    >
                      {{ $t("user.resetPassword") }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </tableOpts>
            </template>
          </el-table-column> -->
        </JADP-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JADP-Form v-if="formVisible" ref="JADPForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <ImportBox
      v-if="importBoxVisible"
      ref="importBox"
      @refresh="reset()"
      @ImportPreviewApi="ImportPreviewApi"
      @importDataApi="importDataApi"
      @TemplateDownload="TemplateDownload"
      :URL="URL"
    />
    <ResetPwdForm
      v-if="resetFormVisible"
      ref="ResetPwdForm"
      @refreshDataList="initData"
    />
  </div>
</template>

<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JADPForm from "./Form";
import ExportBox from "./ExportBox";
import ResetPwdForm from "./ResetPassword";
import { previewDataInterface } from "@/api/systemData/dataInterface";

export default {
  components: { JADPForm, ExportBox, ResetPwdForm },
  data() {
    return {
      //导入属性
      importBoxVisible: false,
      URL: "students/Xy_ks_students",
      showAll: false,
      resetFormVisible: false,
      query: {
        account: undefined,
        realname: undefined,
        mobilephone: undefined,
        companytype: undefined,
        company: undefined,
        accountstatus: undefined
      },
      defaultProps: {
        children: "children",
        label: "fullName"
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "account", label: "用户名" },
        { prop: "realname", label: "姓名" },
        { prop: "gender", label: "性别" },
        { prop: "title", label: "职称" },
        { prop: "mobilephone", label: "手机号码" },
        { prop: "certificatesnumber", label: "身份证号码" },
        { prop: "education", label: "学历" },
        { prop: "company", label: "工作单位" },
        { prop: "companytype", label: "单位类型" }
      ],
      companytypeOptions: [],
      accountstatusOptions: []
    };
  },
  computed: {},
  created() {
    this.initData();
    this.getcompanytypeOptions();
    this.getaccountstatusOptions();
  },
  methods: {
    ImportPreviewApi(val) {
      let query = { fileName: val };
      request({
        url: `/api/students/Xy_ks_students/ImportPreview`,
        method: "get",
        data: query
      })
        .then(res => {
          this.$refs.importBox.list = res.data.dataRow;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    importDataApi(val) {
      let query = { list: val,organizeId :this.$store.getters.organizeId };
      request({
        url: `/api/students/Xy_ks_students/ImportData`,
        method: "post",
        data: query
      })
        .then(res => {
          this.$refs.importBox.result = res.data;
          this.$refs.importBox.resultList = res.data.failResult;
          this.$refs.importBox.btnLoading = false;
          this.$refs.importBox.active++;
        })
        .catch(() => {
          this.$refs.importBox.btnLoading = false;
        });
    },
    TemplateDownload() {
      request({
        url: `/api/students/Xy_ks_students/templateDownload`,
        method: "post"
      }).then(res => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
      });
    },
    uploadForm() {
      this.importBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.importBox.init(this.columnList);
      });
    },
    getcompanytypeOptions() {
      getDictionaryDataSelector("913e8d577af546529ee022e6523fcf37").then(
        res => {
          this.companytypeOptions = res.data.list;
        }
      );
    },
    getaccountstatusOptions() {
      getDictionaryDataSelector("a7fa40d83e014271a63271a16e96885f").then(
        res => {
          this.accountstatusOptions = res.data.list;
        }
      );
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        organizeId:this.$store.getters.organizeId
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      request({
        url: `/api/students/Xy_ks_students`,
        method: "get",
        data: query
      }).then(res => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning"
      })
        .then(() => {
          request({
            url: `/api/students/Xy_ks_students/${id}`,
            method: "DELETE"
          }).then(res => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              }
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id);
      this.multipleSelection = res;
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500
        });
        return;
      }
      const ids = this.multipleSelection.join();
      this.$confirm("您确定要删除这些数据吗, 是否继续？", "提示", {
        type: "warning"
      })
        .then(() => {
          request({
            url: `/api/students/Xy_ks_students/batchRemove/${ids}`,
            method: "DELETE"
          }).then(res => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              }
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JADPForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query };
      request({
        url: `/api/students/Xy_ks_students/Actions/Export`,
        method: "GET",
        data: query
      }).then(res => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      };
      this.initData();
    },
    handleResetPwd(id, account) {
      this.resetFormVisible = true;
      this.$nextTick(() => {
        this.$refs.ResetPwdForm.init(id, account);
      });
    }
  }
};
</script>
